package com.pinecone.controller;

import java.util.List;
import java.util.Map;


import com.pinecone.dto.UserLoginDTO;
import com.pinecone.dto.UserRegisterDTO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pinecone.entity.SysUserEntity;
import com.pinecone.service.SysUserService;
import com.pinecone.common.utils.PageUtils;
import com.pinecone.common.utils.Query;
import com.pinecone.common.utils.R;




/**
 * 系统用户表
 * 
 * @author hushangjie
 * @email 979783618@qq.com
 * @date 2017-09-27 21:17:59
 */
@Api(description = "系统用户相关接口",produces = "application/json")
@RestController
@RequestMapping("sys_user")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;


	/**
	 * 列表
	 */
	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@GetMapping("")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		if (params.get("page") != null && params.get("limit")!= null){
			List<SysUserEntity> sysUserList = sysUserService.queryList(query);
			int total = sysUserService.queryTotal(query);
			
			PageUtils pageUtil = new PageUtils(sysUserList, total, query.getLimit(), query.getPage());
			
			return R.ok().put("page", pageUtil);
		}else{
			List<SysUserEntity> sysUserList = sysUserService.queryList(query);
			return R.ok().put("list", sysUserList);
		}
		
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/{userId}")
	public R info(@PathVariable("userId") Integer userId){
		SysUserEntity sysUser = sysUserService.queryObject(userId);
		
		return R.ok().put("sysUser", sysUser);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("")
	public R save(@RequestBody SysUserEntity sysUser){
		sysUserService.save(sysUser);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@PutMapping("")
	public R update(@RequestBody SysUserEntity sysUser){
		sysUserService.update(sysUser);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping("")
	public R delete(@RequestBody Integer[] userIds){
		sysUserService.deleteBatch(userIds);
		
		return R.ok();
	}
	
}
